package logic

import (
	"aftersale/global/model"
	"context"
	"fmt"
	"time"

	"aftersale/aftersale"
	"aftersale/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AfterSaleOrderCreateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAfterSaleOrderCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AfterSaleOrderCreateLogic {
	return &AfterSaleOrderCreateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AfterSaleOrderCreateLogic) AfterSaleOrderCreate(in *aftersale.AfterSaleOrderCreateRequest) (*aftersale.AfterSaleOrderCreateResponse, error) {
	// todo: add your logic here and delete this line
	var order model.Order
	model.DB.Where("order_id = ? and user_id = ? and order_status in (1,2)", in.OrderId, in.UserId).Find(&order)
	if order.OrderId == 0 {
		return nil, fmt.Errorf("当前订单无法申请售后")
	}
	var after = model.AfterSaleOrder{
		OrderId:         order.OrderId,
		UserId:          int32(in.UserId),
		ProofUrl:        in.Url,
		AfterSaleStatus: "0",
		ApplyTime:       time.Now(),
	}
	model.DB.Create(&after)
	return &aftersale.AfterSaleOrderCreateResponse{}, nil
}
